package com.gin.no_gay_alliance.fragment.config;

import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.databinding.FragmentConfigUiBinding;
import com.gin.no_gay_alliance.enums.ThemeNga;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.manager.ThemeManager;

/**
 * 界面配置
 */
public class ConfigUiFragment extends BaseBindingFragment<FragmentConfigUiBinding> implements RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        // 选中项目修改时，修改选择的主题
        b.rgTheme.setOnCheckedChangeListener(this);
        b.rgTheme.clearCheck();
        // 根据当前选择的主题设置单选框的初始状态
        ThemeManager themeManager = MyApplication.app.managerFactory.getThemeManager();
        ThemeNga theme = themeManager.getTheme();
        switch (theme) {
            case GREEN:
                b.rgTheme.check(R.id.rb_theme_green);
                break;
            case BLACK:
                b.rgTheme.check(R.id.rb_theme_black);
                break;
            case YELLOW:
            default:
                b.rgTheme.check(R.id.rb_theme_yellow);
        }
        // 是否跟随系统
        b.cbFollowSystemUi.setChecked(themeManager.getFollowUi());
        b.cbFollowSystemUi.setOnCheckedChangeListener(this);
    }

    /**
     * RadioGroup切换选中项目的回调
     *
     * @param group     the group in which the checked radio button has changed
     * @param checkedId the unique identifier of the newly checked radio button
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (group == b.rgTheme) {
            ThemeManager themeManager = MyApplication.app.managerFactory.getThemeManager();
            if (checkedId == R.id.rb_theme_green) {
                themeManager.setTheme(ThemeNga.GREEN);
            }
            if (checkedId == R.id.rb_theme_yellow) {
                themeManager.setTheme(ThemeNga.YELLOW);
            }
            if (checkedId == R.id.rb_theme_black) {
                themeManager.setTheme(ThemeNga.BLACK);
            }
        }
    }

    /**
     * Called when the checked state of a compound button has changed.
     *
     * @param buttonView The compound button view whose state has changed.
     * @param isChecked  The new checked state of buttonView.
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView == b.cbFollowSystemUi) {
            MyApplication.app.managerFactory.getThemeManager().setFollowUi(isChecked);
        }
    }
}